package com.myapp.Data;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.util.Log;

import static android.content.Context.WIFI_SERVICE;

/**
 * 网络状态类
 */
public class NetState {
    private static final String TAG = "NetState";
    private WifiInfo currentWifiInfo;
    private static Context mContext;

    private static class WifiStateHolder{
        private static final NetState INSTANCE = new NetState();
    }

    public static NetState getInstance(){
        if (mContext == null){
            throw new RuntimeException("context == null");
        }
        return WifiStateHolder.INSTANCE;
    }

    public static void init(Context context) {
        mContext = context.getApplicationContext();
    }

    private WifiStateCallback wifiStateCallback;

    public void setWifiStateCallback(WifiStateCallback wifiStateCallback) {
        this.wifiStateCallback = wifiStateCallback;
    }

    public interface WifiStateCallback{
        void onWifiChange(WifiInfo info);
    }

    public void registBroadcast(){
        IntentFilter filter = new IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        mContext.registerReceiver(mReceiver,filter);
    }

    public void unRegisterBroadcast(){
        try{
            mContext.unregisterReceiver(mReceiver);
        }catch (IllegalArgumentException ex){
            //广播没有注册
            ex.printStackTrace();
        }

    }

    private BroadcastReceiver mReceiver = new BroadcastReceiver()  {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action == null) {
                return;
            }
            switch (action) {
                case WifiManager.NETWORK_STATE_CHANGED_ACTION:
                    WifiInfo wifiInfo = intent.getParcelableExtra(WifiManager.EXTRA_WIFI_INFO);
                    if (wifiStateCallback != null){
                        // currentWifiInfo = wifiInfo;
                        wifiStateCallback.onWifiChange(currentWifiInfo);
                        Log.i("onReceive: ", "onReceive: "+"wifiInfo:"+wifiInfo+
                                "  currentWifiInfo:"+currentWifiInfo);
                    }
                    break;
            }
        }
    };

    public boolean isWifiConnected() {
        currentWifiInfo = getCurrentWifiInfo();
        Log.i(TAG, "isWifiConnected: "+currentWifiInfo);
        return !(currentWifiInfo.getSSID().equals("<unknown ssid>")||currentWifiInfo==null);
    }

    public WifiInfo getCurrentWifiInfo() {
        WifiManager wm = (WifiManager)mContext.getApplicationContext().getSystemService(WIFI_SERVICE);
        if (wm != null) {
            currentWifiInfo = wm.getConnectionInfo();
        }
        Log.i(TAG, "getCurrentWifiInfo: "+currentWifiInfo);
        return currentWifiInfo;
    }

    public WifiManager getWifiManger(){
        return  (WifiManager)mContext.getApplicationContext().getSystemService(WIFI_SERVICE);
    }

    /**
     * 获取SSID

     * @return  WIFI 的SSID
     */
    public String getWIFISSID() {
        String ssid="unknown id";

        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.O||Build.VERSION.SDK_INT==Build.VERSION_CODES.P) {

            WifiManager mWifiManager = (WifiManager) mContext.getApplicationContext().getSystemService(Context.WIFI_SERVICE);


            assert mWifiManager != null;
            WifiInfo info = mWifiManager.getConnectionInfo();

            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
                Log.i(TAG, "getWIFISSID1: "+info);
                return info.getSSID();

            } else {
                Log.i(TAG, "getWIFISSID2: "+info);
                return info.getSSID().replace("\"", "");
            }
        } else if (Build.VERSION.SDK_INT==Build.VERSION_CODES.O_MR1){

            ConnectivityManager connManager = (ConnectivityManager) mContext.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
            assert connManager != null;
            NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
            if (networkInfo.isConnected()) {
                if (networkInfo.getExtraInfo()!=null){
                    Log.i(TAG, "getWIFISSID3: "+networkInfo);
                    return networkInfo.getExtraInfo().replace("\"","");
                }
            }
        }
        return ssid;
    }

}
